package com.msb.servicemap.remote;

import com.msb.internalcommon.constant.AmapConfigConstants;
import com.msb.internalcommon.dto.ResponseResult;
import com.msb.internalcommon.request.PointRequest;
import com.msb.internalcommon.request.PointsDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.net.URL;

/**
 * @Auther: jiudianliu
 * @Date: 2023/7/9 - 07 - 09 - 13:30
 * @Description: com.msb.servicemap.remote
 * @version: 1.0
 */
@Service
public class PointClient {

    @Value("${map.key}")
    private String key ;

    @Value("${map.sid}")
    private String sid ;

    @Autowired
    private RestTemplate restTemplate ;


    public ResponseResult upload(PointRequest pointRequest){
        //拼装请求的url
        //网址示例：?keywords=北京&subdistrict=2&key=<用户的key>
        StringBuilder url = new StringBuilder();
        url.append(AmapConfigConstants.POINT_UPLOAD);
        url.append("?");
        url.append("key="+key);
        url.append("&");
        url.append("sid="+sid);
        url.append("&");
        url.append("tid="+pointRequest.getTid());
        url.append("&");
        url.append("trid="+pointRequest.getTrid());
        url.append("&");
        url.append("points=");
        PointsDTO[] points = pointRequest.getPoints();
        url.append("%5B") ;
        for (PointsDTO p : points){
            url.append("%7B");
            String locatetime = p.getLocatetime();
            String location = p.getLocation();
            url.append("%22location%22");
            url.append("%3A");
            url.append("%22" + location + "%22");
            url.append("%2C");


            url.append("%22locatetime%22");
            url.append("%3A");
            url.append(locatetime);
            url.append("%7D");
        }
        url.append("%5D") ;
        System.out.println("上传位置请求：" + url.toString());
        ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(URI.create(url.toString()), null, String.class);
        System.out.println("上传位置响应：" + stringResponseEntity.getBody());

        return ResponseResult.success(" ") ;
    }
}
